using System;
using System.Web.UI;
using Microsoft.SharePoint.Utilities;

namespace BilSimser.SharePoint.Common.Controls
{
	public class OWSDateField : OWSBase
	{
		#region construction

		/// <summary>
		/// Default constructor
		/// </summary>
		public OWSDateField()
		{
		}

		#endregion

		#region custom properties

		/// <summary>
		/// Gets or set the number date in a .NET date time format. 
		/// </summary>
		public DateTime DateTime
		{
			get { return SPUtility.CreateSystemDateTimeFromXmlDataDateTimeFormat(Value); }
			set { Value = SPUtility.CreateISO8601DateTimeFromSystemDateTime(value); }
		}

		/// <summary>
		/// Gets or set whether or not this control presents a date only value.
		/// </summary>
		public bool DateOnly
		{
			get
			{
				if (ViewState["DateOnly"] == null)
					DateOnly = false;
				return (bool) ViewState["DateOnly"];
			}
			set { ViewState["DateOnly"] = value; }
		}

		/// <summary>
		/// Gets or set whether or not to hide descriptions.
		/// </summary>
		public bool HideDescription
		{
			get
			{
				if (ViewState["HideDescription"] == null)
					HideDescription = false;
				return (bool) ViewState["HideDescription"];
			}
			set { ViewState["HideDescription"] = value; }
		}

		#endregion

		#region overridden properties

		/// <summary>
		/// Gets the test string representation of the date.
		/// </summary>
		public override string Text
		{
			get
			{
				if (DateOnly)
					return DateTime.ToString("dddd, MMMM d, yyyy");
				return DateTime.ToString("dddd, MMMM d, yyyy h:mm tt");
			}
		}

		#endregion

		#region overridden methods

		/// <summary>
		/// Renders the OWS Control
		/// </summary>
		/// <param name="wtr">writes out the text control information</param>
		protected override void Render (HtmlTextWriter wtr)
		{
			wtr.Write(
				"<SCRIPT>" +
					"fld=new DateField(frm, '{0}', '{1}', '{2}'); " +
					"fld.fDateOnly = {3}; " +
					"fld.fHideDescription = {4}; " +
					"fld.caltype = 1; " +
					"fld.fRequired = {5}; " +
					"fld.BuildUI(); " +
					"</SCRIPT>",
				UniqueID,
				Display,
				JavaScriptValue,
				DateOnly.ToString().ToLower(),
				HideDescription.ToString().ToLower(),
				Required.ToString().ToLower()
				);
		}

		#endregion
	}
}